local xianlue = fk.CreateSkill{
  name = "mod__xianlue"
}
local U = require "packages/utility/utility"
local xianlue_ = {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.RoundStart then
      return true
    else
      return target ~= player and player:getMark("mod__xianlue-turn") == 0 and
      table.contains(U.getPrivateMark(player, "mod__xianlue"), data.card.trueName)
    end
  end,
  on_cost = function(self, event, target, player, data)
    return event == fk.CardUsing or player.room:askForSkillInvoke(player, self.name, data, "#mod__xianlue-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      room:addPlayerMark(player, "mod__xianlue-turn")
      local cards = player:drawCards(2, "mod__xianlue")
      if player.dead then return false end
      cards = table.filter(cards, function(id) return table.contains(player:getCardIds("h"), id) end)
      if #cards > 0 then
        room:askForYiji(player, cards, room.alive_players, self.name, 0, #cards, "#mod__xianlue-give")
        if player.dead then return false end
      end
    end
    local cards = player:getMark("mod__xianlue_cards")
    if type(cards) ~= "table" then
      cards = U.getUniversalCards(room, "t", true)
      room:setPlayerMark(player, "mod__xianlue_cards", cards)
    end
    if #cards == 0 then return false end
    local result = U.askforChooseCardsAndChoice(player, cards, {"OK"}, self.name, "#mod__xianlue-choice")
    U.setPrivateMark(player, "mod__xianlue", {Fk:getCardById(result[1]).name})
  end,
}
local xianlue_refresh = {
  can_refresh = function(self, event, target, player, data)
    return player == target and data == self and player:getMark("@[private]mod__xianlue") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@[private]mod__xianlue", 0)
  end,
}
xianlue:addEffect(fk.RoundStart, xianlue_)
xianlue:addEffect(fk.CardUsing, xianlue_)
xianlue:addEffect(fk.EventLoseSkill, xianlue_refresh)
return xianlue